class Webcomic < ActiveRecord::Base
  belongs_to :creator, :class_name => :user
  has_and_belongs_to_many :categories
  has_many :strips, :dependent => :destroy
  
  validates_length_of :categories, :minimum => 1, :message => "must have at least one category"
  validates_presence_of :title

  def find_most_recent_strip
    most_recent_strip = Strip.getMostRecentStrip(self.id)
    return most_recent_strip
  end

  def find_strip(p_strip_number)
    strip = Strip.find(:all, :conditions => { :webcomic_id => self.id, :strip_number => p_strip_number})
    return strip[0]
  end

  def find_strip_and_neighbors(p_strip_number)
    strip = self.find_strip(p_strip_number)
    prev_strip = self.find_strip(p_strip_number - 1)
    next_strip = self.find_strip(p_strip_number + 1)

    return { :current => strip, :previous => prev_strip, :next => next_strip }
  end
  
  def <=>(wc) 
    return self.title <=> wc.title
  end
end
